package com.csjbot.blackgaga.model.http.product;

import com.csjbot.blackgaga.cart.entity.RobotMenuListBean;
import com.csjbot.blackgaga.cart.entity.RobotSpListBean;
import com.csjbot.blackgaga.cart.pactivity.evaluate.AnswerResponse;
import com.csjbot.blackgaga.global.Constants;
import com.csjbot.blackgaga.model.http.base.BaseImpl;
import com.csjbot.blackgaga.model.http.bean.AdInfo;
import com.csjbot.blackgaga.model.http.bean.PayResponse;
import com.csjbot.blackgaga.model.http.bean.PayResult;
import com.csjbot.blackgaga.model.http.bean.ProductInfo;
import com.csjbot.blackgaga.util.SharedKey;
import com.csjbot.blackgaga.util.SharedPreUtil;
import com.google.gson.Gson;

import io.reactivex.Observer;
import okhttp3.ResponseBody;

/**
 * Created by jingwc on 2017/9/18.
 */

public class ProductImpl extends BaseImpl implements IProduct {

    @Override
    public void getProductInfo(Observer<ProductInfo> observer) {
        scheduler(getRetrofit().getProductInfo()).subscribe(observer);
    }

    @Override
    public ProductInfo getLocalProduct() {
        String str = SharedPreUtil.getString(SharedKey.PRODUCT_SP, SharedKey.PRODUCT_INFO);
        return new Gson().fromJson(str, ProductInfo.class);
    }

    @Override
    public RobotMenuListBean getMenuList() {
        String lan = SharedKey.MENU_LAN.getLanguageStr(Constants.Language.getLanguageStr());
        String str = SharedPreUtil.getString(SharedKey.PRODUCT_MENU_LIST, lan);
        if (str == null) {
            return null;
        } else {
            return new Gson().fromJson(str, RobotMenuListBean.class);
        }
    }

    @Override
    public RobotSpListBean getSpList(String menuid) {
        String lan = SharedKey.SP_LAN.getLanguageStr(Constants.Language.getLanguageStr());
        String str = SharedPreUtil.getString(lan, menuid);
        if (str == null) {
            return null;
        } else {
            return new Gson().fromJson(str, RobotSpListBean.class);
        }
    }

    @Override
    public boolean removeSpList(String name) {
        String lan = SharedKey.SP_LAN.getLanguageStr(name);
        return SharedPreUtil.removeString(lan, name);
    }

    @Override
    public boolean removeMenuList() {
        String lan = SharedKey.MENU_LAN.getLanguageStr(Constants.Language.getLanguageStr());
        return SharedPreUtil.removeString(SharedKey.PRODUCT_MENU_LIST, lan);
    }

    @Override
    public void getAdInfo(Observer<AdInfo> observer) {
        scheduler(getRetrofit().getAdInfo()).subscribe(observer);
    }

    @Override
    public void generateOrder(String json, Observer<PayResponse> observer) {
        scheduler(getRetrofit().generateOrder(getBody(json))).subscribe(observer);
    }

    @Override
    public void sendEvalution(String json, Observer<AnswerResponse> observer) {
        scheduler(getRetrofit().sendEvalution(getBody(json))).subscribe();
    }

    @Override
    public void searchPayResult(String orderid, Observer<PayResult> observer) {
        scheduler(getRetrofit().searchPayResult(orderid)).subscribe(observer);
    }

    @Override
    public void getRobotMenuList(String sn, String language, Observer<RobotMenuListBean> observer) {
        scheduler(getRetrofit().getRobotMenuList(sn,language)).subscribe(observer);
    }


    @Override
    public void dynamicGetRobotSpList(String url, String sn, Observer<RobotSpListBean> observer) {
        scheduler(getRetrofit().dynamicGetRobotSpList(url, sn)).subscribe(observer);
    }

    @Override
    public void fullDynamicGetRobotSpList(String url, Observer<RobotSpListBean> observer) {
        scheduler(getRetrofit().fullDynamicGetRobotSpList(url)).subscribe(observer);
    }

    @Override
    public void getProductImage(String url, Observer<ResponseBody> observer) {
        scheduler(getRetrofit().getProductImage(url)).subscribe(observer);
    }


    @Override
    public ProductService getRetrofit() {
        return getRetrofit(ProductService.class);
    }

}
